package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.UserBanquetRecord;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBanquetRecordDaoImpl extends TempDataObjectDao<UserBanquetRecord> {

    public Map<String, UserBanquetRecord> getFeastBanquetRecordMap(String typeIn) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_banquet_record where `type` in ("+typeIn+") order by openTime DESC";
        Map<String, UserBanquetRecord> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;

            rs = call.executeQuery();
            while (rs.next()) {
                UserBanquetRecord data = parse(rs);
                res.put(data.getBanquetId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }
    public Map<String, UserBanquetRecord> getUserBanquetRecordMap(long userId, String typeIn, long beginTime, int rowMax) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_banquet_record where userId = ? and `type` in("+typeIn+") and openTime > ? ORDER BY openTime desc";
        Map<String, UserBanquetRecord> res = new ConcurrentHashMap<>();
        if(StringUtils.isNullOrEmpty(typeIn)){
            return res;
        }
        try {
            Map<Integer, Integer> typeCount = new HashMap<>();
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++,userId);
            call.setLong(i++, beginTime);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBanquetRecord data = parse(rs);
                int count = typeCount.getOrDefault(data.getType(), 0);
                if(count >= rowMax){
                    continue;
                }
                typeCount.put(data.getType(), 1 + count);
                res.put(data.getBanquetId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean updateData(UserBanquetRecord data){
        return true;
    }

    @Override
    protected boolean addData(UserBanquetRecord data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_banquet_record(banquetId,`type`,userId,record,openTime,overTime,memberCount) values(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getBanquetId());
            call.setInt(i++, data.getType());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getRecord());
            call.setLong(i++, data.getOpenTime());
            call.setLong(i++, data.getOverTime());
            call.setInt(i++, data.getMemberCount());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBanquetRecord parseData(ResultSet rs) throws SQLException {
        UserBanquetRecord data = new UserBanquetRecord();
        data.setBanquetId(rs.getString("banquetId"));
        data.setType(rs.getInt("type"));
        data.setUserId(rs.getLong("userId"));
        data.setRecord(rs.getString("record"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setOverTime(rs.getLong("overTime"));
        data.setMemberCount(rs.getInt("memberCount"));
        return data;
    }
}
